<template>
  <el-skeleton :loading="loading">
    <div class="section" v-if="dataset.length">
      <div>备选：</div>
      <el-tag  size="small"
        :effect="checkedIndex === i - 1 ? 'dark' : 'plain'" 
        v-for="i in dataset.length" :key="i" 
        @click="checkedIndex = i - 1"
      >{{ i }}</el-tag>
      <div class="reset" @click="getQuestionList"><i class="iconfont iconhuanti" />换一批</div>
    </div>
    <div class="content" v-if="dataset[checkedIndex]">
      <div class="title" v-html="dataset[checkedIndex].title"></div>
      <div class="main" v-html="dataset[checkedIndex].html"></div>
      <div class="attribute">
        <div class="difficult">
          <span class="label">难度</span>{{ difficultFilter(dataset[checkedIndex].difficult) }}
        </div>
        <div class="knowledges">
          <span class="label">知识点</span><span v-for="item in dataset[checkedIndex].knowledges" :key="item.id" class="knowledge"> {{ item.name }} </span>
        </div>
      </div>
    </div>
    <cus-empty v-if="!dataset.length" />
  </el-skeleton>
</template>

<script lang="ts">
import { ref } from 'vue';
import { questToHtml } from '@/views/utils/question.directive';
import $ from '$';

export default {
  props: ['id'],
  setup(props) {
    let loading = ref(true);
    let dataset = ref([]);
    let checkedIndex = ref(-1);
    let current = 1;
    const difficultFilter = (v) =>
    [
      { name: "易", id: 11 },
      { name: "较易", id: 12 },
      { name: "中档", id: 13 },
      { name: "较难", id: 14 },
      { name: "难", id: 15 },
    ].find((i) => i.id === v)?.name;
    const getQuestionList = async () => {
      loading.value = true;
      let res: any = await $.http.post('/tiku/question/querySimilar', { id: props.id, current,size: 10 });
      loading.value = false;
      if (res.result) {
        checkedIndex.value = 0;
        dataset.value = res.json.records.map(i => { i.html = questToHtml(i); return i; });
        current = res.json.records.length < 10 || res.json.total <= current * 10 ? 1 : current + 1;
      }
    }
    getQuestionList();

    const save = (resolve) => {
      resolve($.clone(checkedIndex.value > -1 ? dataset.value[checkedIndex.value] : false));
    }

    return { loading, dataset, checkedIndex, getQuestionList, save, difficultFilter };
  }
}
</script>

<style lang="scss" scoped>
.section {
  display: flex;
  line-height: 24px;
  margin-bottom: 20px;
  .el-tag {
    margin-right: 10px;
    cursor: pointer;
  }
  .reset {
    margin-left: auto;
    color: #72b22a;
    font-size: 12px;
    cursor: pointer;
    i {
      font-size: 14px;
      margin-right: 3px;
    }
  }
}
.content {
  .title {
    margin-bottom: 10px;
  }
  .main {
    padding-left: 10px;
  }
  .attribute {
    display: flex;
    margin-top: 20px;
    .knowledges {
      margin-left: 20px;
      .knowledge {
        margin-left: 5px;
      }
    }
    .label {
      display: inline-block;
      height: 20px;
      padding: 4px 10px;
      color: #3abab3;
      font-size: 12px;
      line-height: 20px;
      background: rgba(58, 186, 179, 0.15);
      border-radius: 4px;
      margin-right: 8px;
      white-space: nowrap;
    }
  }
}

:deep(.main) .e-main {
  .e-m-cell {
    display: flex;
    &:not(:last-child) {
      margin-bottom: 10px;
    }
    .e-c-label {
      width: 40px;
    }
    .e-c-group {
      flex: 1 1 40px;
      display: flex;
      flex-wrap: wrap;
      .c-t-item {
        flex: 1;
        white-space: nowrap;
      }
    }
  }
  &-title {
    .e-m-cell {
      margin-bottom: 20px;
      .e-c-label {
        margin-bottom: 5px;
      }
      .e-c-group {
        line-height: 24px;
        text-indent: 20px;
      }
    }
  }
}
</style>